package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService service;

    //添加菜品页面查询分类名称
    @GetMapping("/list")
    public R<List<Category>> get(Integer type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type != null, Category::getType, type);
        List<Category> list = service.list(wrapper);
        return R.success(list);
    }

    //菜品分类分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize) {
        Page<Category> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        service.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }
    //添加菜品分类
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        service.save(category);
        return R.success("莹莹温馨提示：添加分类成功！！");
    }
     //根据id删除菜品分类
    @DeleteMapping
    public R<String> remove(@RequestParam("ids") Long id)  {
        return service.remove(id);
    }
     //修改菜品分类
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        service.updateById(category);
        return R.success("莹莹温馨提示:修改成功！！");
    }
}
